#include "updatedeviceinfo.h"
#include "ui_updatedeviceinfo.h"

ReviseDeviceInfo::ReviseDeviceInfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ReviseDeviceInfo)
{
    ui->setupUi(this);
    this->setWindowFlags(this->windowFlags() &~ Qt::WindowMinMaxButtonsHint);

    /* 数据校验：只能输入数字 */
    ui->IPAddr_1->setValidator(new QIntValidator(0, 255, this));
    ui->IPAddr_2->setValidator(new QIntValidator(0, 255, this));
    ui->IPAddr_3->setValidator(new QIntValidator(0, 255, this));
    ui->IPAddr_4->setValidator(new QIntValidator(0, 255, this));
    ui->Port->setValidator(new QIntValidator(0, 65532, this));

    cusMsg = new CustomMessageBox();

    /* 设备名称修改 */
    connect(ui->deviceName, &QLineEdit::textChanged, this, [=](QString deviceName){
        this->deviceName = deviceName;
    });

    /* 输入校验：范围限制 */
    connect(ui->IPAddr_1, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_1, text, 255);
    });
    connect(ui->IPAddr_2, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_2, text, 255);
    });
    connect(ui->IPAddr_3, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_3, text, 255);
    });
    connect(ui->IPAddr_4, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_4, text, 255);
    });
    connect(ui->Port, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->Port, text, 65532);
    });


    /* 点击完成按钮，回传数据 */
    connect(ui->ok, &QPushButton::clicked, this, [=](){
        /* 判断是否重名 */
        /* 判断是否有设备名称重复 */
        for(int i = 0; i < connectDeviceInfo.size(); i++){
            if(connectDeviceInfo[i].dev.id != this->deviceId && connectDeviceInfo[i].dev.name == ui->deviceName->text())
            {
                cusMsg->show(this, CMsg::CWarning, "设备列表", "设备名称已存在！");
                return;
            }
        }

        this->deviceIp = ui->IPAddr_1->text() + "." + ui->IPAddr_2->text() + "." + ui->IPAddr_3->text() + "." + ui->IPAddr_4->text();
        this->devicePort = ui->Port->text();

        emit ReviseDeviceInfo::sendNewDeviceInfo(this->row, this->deviceName, this->deviceIp, this->devicePort);
        this->close();
    });

}

ReviseDeviceInfo::~ReviseDeviceInfo()
{
    delete ui;
}

/* 获取并渲染当前设备信息 */
void ReviseDeviceInfo::renderCurrentInfo(int row, QString deviceName, QString deviceIp, QString devicePort, QString deviceId){
    /* 渲染设备信息 */
    ui->deviceName->setText(deviceName);
    QStringList ipList = deviceIp.split(".");
    if(ipList.size() == 4){
        ui->IPAddr_1->blockSignals(true);
        ui->IPAddr_2->blockSignals(true);
        ui->IPAddr_3->blockSignals(true);
        ui->IPAddr_4->blockSignals(true);
        ui->IPAddr_1->setText(ipList[0]);
        ui->IPAddr_2->setText(ipList[1]);
        ui->IPAddr_3->setText(ipList[2]);
        ui->IPAddr_4->setText(ipList[3]);
        ui->IPAddr_1->blockSignals(false);
        ui->IPAddr_2->blockSignals(false);
        ui->IPAddr_3->blockSignals(false);
        ui->IPAddr_4->blockSignals(false);
    }

    ui->Port->setText(devicePort);
    /* 写入设备信息 */
    this->deviceId = deviceId;
    this->deviceName = deviceName;
    this->deviceIp = deviceIp;
    this->devicePort = devicePort;
    this->row = row;
}

/* 数据校验 */
void ReviseDeviceInfo::setDataValidator(QLineEdit *obj, QString text, int max){
    if(text.toInt() > max)
        obj->setText(QString::number(max));
    if(text.toInt() == 0)
        obj->setText("0");
    if(text.toInt() > 0 && text.toInt() < max){
        text = QString::number(0 + text.toInt());
        obj->setText(text);
    }
}
